home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_srgp.lha / srgp / src / srgp_input_MAC.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-09  |  12.4 KB  |  438 lines

  1. #include "HEADERS.h"
  2. #include "srgplocal.h"
  3. #include <ctype.h>
  4.  
  5. /** LOW-LEVEL "DRIVERS"
  6.  
  7.    SRGP__activateDevice (deviceID)
  8.       Must be called when device is going from INACTIVE to active, OR
  9.          when device is changed from one active mode to the other.
  10.  
  11.    SRGP__deactivateDevice (deviceID)
  12.       Must be called only when device is going from active to inactive.
  13.  
  14.  
  15.    SRGP__handleRawEvents (boolean in_waitEvent_call)
  16.       This function nevers enters a wait state.
  17.       It examines all the events on the "raw"
  18.          queue: the queue of the underlying graphics package
  19.      (e.g., X11, Mac).
  20.       Exception: it may not handle all the raw events.
  21.          It exits as soon as it sees a valid trigger situation.
  22.       It returns a device ID IF AND ONLY IF...
  23.      1) the appl. is in a call to SRGP_waitEvent(), AND
  24.      2) a valid trigger for a device currently in Event mode
  25.         has been encountered.
  26.       IF it does return a device ID, THEN...
  27.          It automatically sets the proper value for either
  28.          srgp__get_locator_measure or
  29.          srgp__get_keyboard_measure
  30.      in preparation for the application's ensuing call to 
  31.          SRGP_get...()
  32.       Another exception: it may "pass over" some raw events and
  33.          just leave them in the raw queue.
  34.       It will pass over a raw event IF AND ONLY IF...
  35.      1) the appl. is not in a call to SRGP_waitEvent(), AND
  36.      2) the event is a valid trigger for a device
  37.         currently in Event mode.
  38.       Another possibility is that it will discard a raw event
  39.          without processing it at all.
  40.       It will discard a raw event IF AND ONLY IF...
  41.          The event is for a device that is currently inactive.
  42. **/
  43.  
  44. #define BACKSPACE_KEY      8
  45. #define CARRIAGE_RETURN  13   /* hex 0D */
  46.  
  47.  
  48. static RgnHandle cursorRgn;
  49.  
  50. static EventRecord macevent;
  51. static boolean gotEvent;
  52.  
  53. static int which_button;
  54.  
  55. static Handle transData;
  56.  
  57. void
  58. SRGP__initInputDrivers()
  59. {
  60.    cursorRgn = NewRgn();
  61.    
  62.    transData = RGetResource ('KCHR', 0);
  63. }
  64.  
  65.  
  66. static void AllowUserToGrowConsole (void)
  67. {
  68.    long gresult;
  69.    int newheight, newwidth;
  70.    Rect growlimit = {100,100,  3000,3000};
  71.  
  72.    /* This loop ignores all events until a mouse-down occurs.  
  73.       No matter where the
  74.       mouse-down occurs, it initiates a grow interaction!
  75.     */
  76.    DrawGrowIcon (srgpmac__cwindow);
  77.    while (1) {
  78.       gotEvent = WaitNextEvent (everyEvent, &macevent, 1, cursorRgn);
  79.       if ( ! gotEvent)
  80.          continue;
  81.       if (macevent.what == mouseDown) {
  82.      gresult = GrowWindow (srgpmac__cwindow, macevent.where, &growlimit);
  83.      HiliteMenu (0);
  84.      newheight = (gresult>>16);
  85.      newwidth = (gresult&0xffff);
  86.      SizeWindow (srgpmac__cwindow, newwidth, newheight, FALSE);
  87.      SRGP__reactToScreenResize (newwidth, newheight);
  88.      break;
  89.       }
  90.    }
  91. }
  92.  
  93.  
  94.  
  95.  
  96. /** RAW-LEVEL DEACTIVATION OF A DEVICE
  97. Responsible for erasing echo, and resetting device's measure to the
  98.    hardwired default.
  99. Upon entry, the device's cur_mode is its old value (has not been
  100.    changed yet)!  And this procedure does not change it!
  101. **/
  102.  
  103. void
  104. SRGP__deactivateDevice (int device)
  105. {
  106.    switch (device) {
  107.       
  108.     case LOCATOR:
  109.       SRGP__disableLocatorRubberEcho();
  110.       SRGP__disableLocatorCursorEcho();
  111.       srgp__cur_locator_measure.position = 
  112.          SRGP_defPoint(srgp__canvasTable[0].max_xcoord>>1,
  113.                srgp__canvasTable[0].max_ycoord>>1);
  114.       /* Delete all currently queued locator-related raw events. */
  115.       /* ANY WAY TO DO THIS ON MAC? */
  116.       break;
  117.        
  118.     case KEYBOARD:
  119.       SRGP__disableKeyboardEcho();
  120.       srgp__cur_keyboard_measure.buffer[0] = '\0';
  121.       bzero (srgp__cur_keyboard_measure.modifier_chord,
  122.          sizeof(srgp__cur_keyboard_measure.modifier_chord));
  123.       break;
  124.    }
  125. }
  126.  
  127.  
  128.  
  129.  
  130.  
  131. /** RAW-LEVEL ACTIVATION OF A DEVICE
  132. Called whenever:
  133.   a device is placed into EVENT or SAMPLE mode...
  134.      a) when previously inactive
  135.      b) when previously active but in a different mode
  136. Responsible for initiating echo and setting X's selection mask.
  137. Upon entry, the device's echo info and mode has already been set
  138.   to their new values.
  139. **/
  140.  
  141. void
  142. SRGP__activateDevice (int device)
  143. {
  144.    switch (device) {
  145.     case LOCATOR:
  146.       SRGP__disableLocatorCursorEcho();
  147.       SRGP__disableLocatorRubberEcho();
  148.       SRGP__enableLocatorCursorEcho();
  149.       SRGP__enableLocatorRubberEcho();
  150.       break;
  151.        
  152.     case KEYBOARD:
  153.       SRGP__enableKeyboardEcho();
  154.       break;
  155.    }
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162. void
  163. SRGP__updateRawCursorPosition ()
  164. {
  165.    srgp__cur_Xcursor_x = srgp__cur_locator_measure.position.x;
  166.    srgp__cur_Xcursor_y = 
  167.       SCREENFIXED(srgp__cur_locator_measure.position.y);
  168.    /* SORRY, CAN'T "warp the cursor to new position" ON THE Mac */
  169.    SRGP__updateLocatorRubberEcho();
  170. }
  171.  
  172.  
  173.  
  174.  
  175. static void DetermineModifiers (int modifiers, buttonStatus chord[3])
  176. {
  177.    chord[SHIFT] = (modifiers & shiftKey)?DOWN:UP; 
  178.    chord[CONTROL] = (modifiers & controlKey)?DOWN:UP; 
  179.    chord[META] = (modifiers & optionKey)?DOWN:UP; 
  180. }
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187. static inputDevice HandleXButtonEvent(buttonStatus TRANSITION_TYPE)
  188. {
  189.    GrafPtr savedport;
  190.  
  191.    if (srgp__curActiveCanvasId != 0) {
  192.       savedport = thePort;
  193.       SetPort (srgp__canvasTable[0].drawable.win);
  194.       GlobalToLocal (&macevent.where);
  195.       SetPort (savedport);
  196.    }
  197.    else
  198.       GlobalToLocal (&macevent.where);
  199.  
  200.    srgpx__cur_time = TickCount();
  201.    which_button = LEFT_BUTTON; 
  202.    srgp__cur_locator_measure.button_chord[which_button] = TRANSITION_TYPE; 
  203.    srgp__cur_locator_measure.button_of_last_transition = which_button; 
  204.    srgp__cur_locator_measure.position.x = macevent.where.h;
  205.    srgp__cur_locator_measure.position.y = SCREENFIXED(macevent.where.v);
  206.    DetermineModifiers (macevent.modifiers, srgp__cur_locator_measure.modifier_chord); 
  207.    if ((srgp__cur_mode[LOCATOR] == EVENT) && 
  208.        ((srgp__cur_locator_button_mask >> which_button) & 1)) { 
  209.         srgp__get_locator_measure = srgp__cur_locator_measure; 
  210.              return LOCATOR; 
  211.    }
  212.    return NO_DEVICE;
  213. }
  214.  
  215.  
  216.  
  217. static void CheckForMouseMotion (void)
  218. {
  219.    Point p;
  220.    GrafPtr savedport;
  221.    
  222.    if (srgp__curActiveCanvasId != 0) {
  223.       savedport = thePort;
  224.       SetPort (srgp__canvasTable[0].drawable.win);
  225.       GetMouse (&p);
  226.       SetPort (savedport);
  227.    }
  228.    else
  229.       GetMouse (&p);
  230.    
  231.    if ((p.h == srgp__cur_Xcursor_x) &&
  232.        (p.v == srgp__cur_Xcursor_y))
  233.          return;  /* no movement of mouse since we last checked */
  234.          
  235.    srgpx__cur_time = TickCount();
  236.       
  237.    srgp__cur_Xcursor_x = p.h;
  238.    srgp__cur_Xcursor_y = p.v;
  239.    if (srgp__cur_mode[LOCATOR] != INACTIVE)
  240.       SRGP__updateLocatorRubberEcho();
  241.                   
  242.    srgp__cur_locator_measure.position.x = srgp__cur_Xcursor_x;
  243.    srgp__cur_locator_measure.position.y = SCREENFIXED(srgp__cur_Xcursor_y);
  244. }
  245.  
  246.  
  247.  
  248. /*!*/
  249. /** SRGP_interpret_X_event
  250.       This function nevers enters a wait state.
  251.       It examines all the events on the "raw"
  252.          queue: the queue of the underlying graphics package
  253.      (e.g., X11, Mac).
  254.       Exception: it may not handle all the raw events.
  255.          It exits as soon as it sees a valid trigger situation.
  256.       It returns a device ID IF AND ONLY IF...
  257.      1) the appl. is in a call to SRGP_waitEvent(), AND
  258.      2) a valid trigger for a device currently in Event mode
  259.         has been encountered.
  260.       IF it does return a device ID, THEN...
  261.          It automatically sets the proper value for either
  262.          srgp__get_locator_measure or
  263.          srgp__get_keyboard_measure
  264.      in preparation for the application's ensuing call to 
  265.          SRGP_get...()
  266.       Another exception: it may "pass over" some raw events and
  267.          just leave them in the raw queue.
  268.       It will pass over a raw event IF AND ONLY IF...
  269.      1) the appl. is not in a call to SRGP_waitEvent(), AND
  270.      2) the event is a valid trigger for a device
  271.         currently in Event mode.
  272.       Another possibility is that it will discard a raw event
  273.          without processing it at all.
  274.       It will discard a raw event IF AND ONLY IF...
  275.          The event is for a device that is currently inactive.
  276.  
  277. TABLE OF SITUATIONS:
  278.    LOCATOR  KEYBOARD  in_wait_event                Masks
  279.    -------  --------  -------------   ------------------------------------
  280.    Inactive DONTCARE  DONTCARE        KEYPRESS
  281.    Sample   DONTCARE     NO        KEYPRESS, MOTION, BUTTONS
  282.    Sample   DONTCARE     YES        KEYPRESS, MOTION, BUTTONS
  283.    Event    DONTCARE    NO        KEYPRESS, MOTION
  284.    Event    DONTCARE    YES        KEYPRESS, MOTION, BUTTONS
  285.  
  286. **/
  287. int
  288. SRGP__handleRawEvents (boolean in_wait_event, boolean unused)
  289. {
  290.    int xr, yr, xstrcount;
  291.    char buffer[10];
  292.    int mask;
  293.    WindowPtr winptr;
  294.    inputDevice id;
  295.    long gresult;
  296.    int whichmenu, whichitem;
  297.    int newheight, newwidth;
  298.    Rect boundrect = {0,40,3000,3000};
  299.    unsigned int keycode;
  300.    static long keyTransState=0;
  301.  
  302.  
  303.  while (1) {
  304.    gotEvent = WaitNextEvent (everyEvent, &macevent, 1, cursorRgn);
  305.    
  306.    CheckForMouseMotion();
  307.    if ( ! gotEvent)
  308.       return NO_DEVICE;
  309.    
  310.    switch (macevent.what) {
  311.             
  312.     case app4Evt:  /* my empirical observation is that this is the new "null" code */
  313.     case nullEvent:
  314.       return NO_DEVICE;
  315.       
  316.     case mouseUp:
  317.       if (id = HandleXButtonEvent (UP))
  318.          return id;
  319.       break;
  320.       
  321.     case mouseDown:
  322.       switch (FindWindow(macevent.where, &winptr)) {
  323.        case inMenuBar:
  324.      gresult = MenuSelect (macevent.where);
  325.          whichmenu = (gresult>>16);
  326.          whichitem = (gresult&0xffff);
  327.      switch (whichmenu) {
  328.       case 200:
  329.         switch (whichitem) {
  330.          case 1:
  331.            AllowUserToGrowConsole();
  332.         }
  333.      }
  334.      break;
  335.       case inDrag:
  336.          if (winptr == srgpmac__cwindow) {
  337.             /****** SRGP__deactivateApplColorTable(); */
  338.             DragWindow (winptr, macevent.where, &screenBits.bounds);
  339.             /****** SRGP__activateApplColorTable(); */
  340.          }
  341.          break;
  342.        case inGoAway:
  343.          if (TrackGoAway (winptr, macevent.where)) {
  344.             SRGP_end();
  345.             ExitToShell();
  346.          }
  347.          break;
  348.        case inSysWindow:
  349.      SystemClick (&macevent, winptr);
  350.      break;
  351.        case inContent:
  352.          if (id = HandleXButtonEvent (DOWN))
  353.             return id;
  354.      break;
  355.       }
  356.       break;
  357.       
  358.     case updateEvt: 
  359.       if (winptr == srgpmac__cwindow) {
  360.          BeginUpdate (winptr);
  361.          EndUpdate (winptr);
  362.       }
  363.       return NO_DEVICE;
  364.       
  365.     case activateEvt:
  366.       if ((macevent.modifiers & activeFlag) != 0) 
  367.          /* THIS IS AN ACTIVATION OF THE MAC WINDOW */
  368.          SRGP__activateApplColorTable();
  369.       else
  370.          SRGP__deactivateApplColorTable();
  371.       break;
  372.     
  373.     case keyDown:
  374.       /* buffer[0] = macevent.message & charCodeMask; */
  375.       keycode = ((macevent.message & keyCodeMask)>>8) | (macevent.modifiers&0xff00);
  376.       buffer[0] = (char) KeyTrans (*transData, keycode, &keyTransState);
  377.       if (srgp__cur_mode[KEYBOARD] == INACTIVE) 
  378.      break;
  379.       
  380.       srgpx__cur_time = macevent.when;
  381.  
  382.       /* ONLY IF KEYBOARD IS ACTIVE */
  383.       if (srgp__cur_keyboard_processing_mode == RAW) {
  384.      srgp__cur_keyboard_measure.buffer[0] = *buffer;
  385.      srgp__cur_keyboard_measure.buffer[1] = '\0';
  386.          DetermineModifiers (macevent.modifiers, srgp__cur_keyboard_measure.modifier_chord);
  387.      if (srgp__cur_mode[KEYBOARD] == EVENT) {
  388.         strcpy (srgp__get_keyboard_measure.buffer, 
  389.             srgp__cur_keyboard_measure.buffer);
  390.         bcopy (srgp__cur_keyboard_measure.modifier_chord,
  391.            srgp__get_keyboard_measure.modifier_chord,
  392.            sizeof(srgp__get_keyboard_measure.modifier_chord));
  393.         return KEYBOARD;
  394.      }
  395.       }
  396.       else /* EDIT processing mode */
  397.      switch (buffer[0]) {
  398.       case CARRIAGE_RETURN:
  399.         if (srgp__cur_mode[KEYBOARD] == EVENT)
  400.            strcpy (srgp__get_keyboard_measure.buffer, 
  401.                srgp__cur_keyboard_measure.buffer);
  402.         srgp__cur_keyboard_measure.buffer[0] = '\0';
  403.         srgp__cur_keyboard_measure_length = 0;
  404.         SRGP__updateKeyboardEcho();
  405.         if (srgp__cur_mode[KEYBOARD] == EVENT)
  406.            return KEYBOARD;
  407.         break;
  408.         
  409.       case BACKSPACE_KEY:
  410.         if (srgp__cur_keyboard_measure_length > 0) {
  411.            srgp__cur_keyboard_measure_length =
  412.           srgp__cur_keyboard_measure_length - 1;
  413.            srgp__cur_keyboard_measure.buffer
  414.           [srgp__cur_keyboard_measure_length] =
  415.              '\0';
  416.            SRGP__updateKeyboardEcho();
  417.         }
  418.         break;
  419.         
  420.       default:
  421.             /* CHECK: IS THE KEY PRINTABLE ASCII? */
  422.         if ((isprint(*buffer)) &&
  423.         (srgp__cur_keyboard_measure_length < MAX_STRING_SIZE)) {
  424.            srgp__cur_keyboard_measure.buffer
  425.           [srgp__cur_keyboard_measure_length] = 
  426.              *buffer;
  427.            srgp__cur_keyboard_measure_length++;
  428.            srgp__cur_keyboard_measure.buffer
  429.           [srgp__cur_keyboard_measure_length] = 
  430.              '\0';
  431.            SRGP__updateKeyboardEcho();
  432.         }
  433.         break;
  434.      }
  435.    }
  436.  }
  437. }
  438.